package com.gdcp.service.impl;

import com.gdcp.beans.Category;
import com.gdcp.beans.Role;
import com.gdcp.beans.Shop;
import com.gdcp.beans.User;
import com.gdcp.common.ResponseModel;
import com.gdcp.config.RequestHolder;
import com.gdcp.mapper.CategoryMapper;
import com.gdcp.service.CategoryService;
import com.gdcp.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    ResponseModel responseModel;
    @Autowired
    ShopService shopService;

    @Override
    public Map<String,Object> queryByParentId_one() {
        Map<String, Object> modelMap=new HashMap<>();
        List<Category> categories_one;
        categories_one=categoryMapper.queryByParentId(1);
        modelMap.put("category_one",categories_one);
        return modelMap;
    }

    @Override
    public List<Category> queryByParentId(Integer level) {
        List<Category> categories_one;
        List<Category> categories_two;
        List<Category> categories_three;
        List<Category> categories_four;
        List<Category> listList_two=new ArrayList<>();
        List<Category> listList_three=new ArrayList<>();
        List<Category> listList_four=new ArrayList<>();
        if (level>=1){
            categories_one=categoryMapper.queryByParentId(1);

            if (level>=2) {
                for (Category category_one : categories_one) {
                    categories_two = categoryMapper.queryByParentId(category_one.getId());
                    if (categories_two!=null&&level==2){
                        for (Category category:categories_two){
                            listList_two.add(category);
                        }
                    }
                    if (categories_two!=null&&level>=3){
                        for (Category category_two:categories_two){
                            categories_three=categoryMapper.queryByParentId(category_two.getId());
                            if (categories_three!=null&&level==3) {
                                for (Category category : categories_three) {
                                    listList_three.add(category);
                                }
                            }
                            if (categories_three!=null&&level>=4){
                                for (Category category_three:categories_three){
                                    categories_four=categoryMapper.queryByParentId(category_three.getId());
                                    if (categories_four!=null&&level==4) {
                                        for (Category category : categories_four) {
                                            listList_four.add(category);
                                            //System.out.println("category"+category);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if (level==2){
            return listList_two;
        }

        if (level == 3) {
            return listList_three;
        }

        if (level == 4) {
            return listList_four;
        }

        return null;
    }

    @Override
    public List<Category> queryById(Integer id) {
        return categoryMapper.queryByParentId(id);
    }

    @Override
    public Map<String, Object> findAll(Map<String, Object> model) {
        Map<String,Object> result = new HashMap<>();
        int total=categoryMapper.findPageBreakByConditionByShopId(0).size();
        List<User> rows=categoryMapper.findAll(model);
        if (rows.size()==0){
            int count= (int) model.get("a");
            count=count-(int) model.get("b");
            if (count>0){
                model.put("a",count);
                rows=categoryMapper.findAll(model);
            }
        }
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    public Map<String, Object> searchInformationByShopId(Map<String, Object> model, Integer shop_id) {
        Map<String,Object> result = new HashMap<>();
        int total=categoryMapper.searchSizeByShopId(shop_id).size();
        List<Category> rows=categoryMapper.searchInformationByShopId(model);

        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    public Category getInformation(Integer parentId) {
        return categoryMapper.getInformation(parentId);
    }

    @Override
    public List<Category> queryByShopId(Integer shop_id) {
        return categoryMapper.queryAllByShopId(shop_id);
    }


    @Override
    public Map<String,Object> findPageBreakByCondition(Map<String, Object> model, Integer shop_id) {
        //bootstrap-table要求服务器返回的json须包含：totlal，rows
        Map<String,Object> result = new HashMap<>();
        int total=categoryMapper.findPageBreakByConditionByShopId(shop_id).size();
        List<Category> rows=categoryMapper.findPageBreakByCondition(model);
        if (rows.size()==0){
            int count= (int) model.get("a");
            count=count-(int) model.get("b");
            if (count>0){
                model.put("a",count);
                rows=categoryMapper.findPageBreakByCondition(model);
            }
        }
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    @Transactional(rollbackFor = { Exception.class })
    public Map<String,Object> removeByPrimaryKey(Integer[] ids) {
        try{
            for (Integer id:ids) {
                categoryMapper.removeByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return responseModel.setModel(false,"删除["+ids.length+"条记录失败"+e.toString());
        }
        return responseModel.setModel(true,"成功删除["+ids.length+"条记录");
    }

    @Override
    public Map<String,Object> searchInformation(Map<String, Object> param) {
        Map<String,Object> result = new HashMap<>();
        int total=categoryMapper.searchInformation(null).size();
        List<Category> rows=categoryMapper.searchInformation(param);

        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    public Map<String, Object> insert(Category category) {
        if (category.getShop_id()!=null&&category.getShop_id()!=0){
            int count=categoryMapper.queryAllByShopId(category.getShop_id()).size();
            if (!(count<5)){
                return responseModel.setModel(false,"该店铺分类数量已经有五个了，不可再加！");
            }
        }

        User user= (User) RequestHolder.getSession("user");
        Shop shop=shopService.getInformation(user.getShop_id());
        if (user.getShop_id()!=0&&shop==null){
            return responseModel.setModel(true,"插入失败，店铺id为"+user.getShop_id()+"不存在");
        }

        category.setShop_id(user.getShop_id());
        try {
            categoryMapper.insert(category);
        }catch (Exception e){
            e.printStackTrace();
            return responseModel.setModel(false,"插入失败");
        }
        return responseModel.setModel(true,"插入成功");
        /*for (Role role: user.getRoles()){
            if (role.getName().equals("ROLE_ADMIN")) {
                category.setShop_id(user.getShop_id());
                try {
                    categoryMapper.insert(category);
                    return responseModel.setModel(true,"插入成功");
                }catch (Exception e){
                    e.printStackTrace();
                    return responseModel.setModel(false,"插入失败");
                }
            }
            if (role.getName().equals("ROLE_SHOP_ADMIN")) {
                //Category categoryInfo = categoryMapper.getInformation(category.getParent_id());
                //category.setName(categoryInfo.getName());
                category.setShop_id(user.getShop_id());
                try {
                    categoryMapper.insert(category);
                    return responseModel.setModel(true,"插入成功");
                }catch (Exception e){
                    e.printStackTrace();
                    return responseModel.setModel(false,"插入失败");
                }
            }
        }*/
    }

    @Override
    public Map<String,Object> updateByCategory(Category category) {
        try{
            categoryMapper.updateByPrimaryKey(category);
        }catch (Exception e){
            e.printStackTrace();
            return responseModel.setModel(false,"修改失败");
        }
        return responseModel.setModel(true,"修改成功");
    }

    //@Override
    //public Category getInformation(Integer id) {
    //    return categoryMapper.getInformation(id);
    //}

    @Override
    public List<Category> queryByAll() {
        return categoryMapper.queryByAll();
    }

    @Override
    public List<Category> queryAllByShopId(Integer shopId) {
        return categoryMapper.queryAllByShopId(shopId);
    }
}
